package org.cuit.epoch.seon.top;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

// 容器节点 - 文件夹
class Directory implements FileSystemComponent {
    private String name;
    private List<FileSystemComponent> components = new ArrayList<>();

    public Directory(String name) {
        this.name = name;
    }

    public void addComponent(FileSystemComponent component) {
        components.add(component);
    }

    public void removeComponent(FileSystemComponent component) {
        components.remove(component);
    }

    @Override
    public void showDetails(String indent) {
        System.out.println(indent + "Directory: " + name);
        for (FileSystemComponent component : components) {
            component.showDetails(indent + "  ");
        }
    }

    @Override
    public int getSize() {
        int totalSize = 0;
        for (FileSystemComponent component : components) {
            totalSize += component.getSize();
        }
        return totalSize;
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public void rename(String newName) {
        this.name = newName;
    }

    @Override
    public void delete() {
        System.out.println("Deleting directory: " + name);
        components.clear(); // 清空目录内容
    }

    public Optional<FileSystemComponent> findComponent(String name) {
        for (FileSystemComponent component : components) {
            if (component.getName().equals(name)) {
                return Optional.of(component);
            }
            if (component instanceof Directory) {
                Optional<FileSystemComponent> found = ((Directory) component).findComponent(name);
                if (found.isPresent()) {
                    return found;
                }
            }
        }
        return Optional.empty();
    }
}
